package com.freshcity.manager
{
	import com.pblabs.engine.PBE;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import com.freshcity.IView;

	public class PopupManager
	{
		private static var _topDisplayObject:DisplayObject;
		
		public static function addPopUp(arg1:DisplayObject, arg2:String="window", arg3:Boolean=false):void
		{
			if (arg1 && LayerManager.windowLayer.contains(arg1) == false) 
			{
				arg1.x = (PBE.mainStage.stageWidth - arg1.width) / 2;
				arg1.y = (PBE.mainStage.stageHeight - arg1.height) / 2;
				if (arg2 != "window") 
				{
					if (arg1 is IView) 
					{
						(arg1 as IView).show();
					}
				}
				else 
				{
					LayerManager.windowLayer.addChild(arg1);
				}
				_topDisplayObject = arg1;
				if (arg3) 
				{
					createTempCover(arg1, 0, 0, true);
				}
			}
		}
		
		public static function centerPopup(arg1:DisplayObject):void
		{
			if (arg1) 
			{
				arg1.x = (PBE.mainStage.stageWidth - arg1.width) / 2;
				arg1.y = (PBE.mainStage.stageHeight - arg1.height) / 2;
			}
		}
		
		public static function isTop(arg1:DisplayObject):Boolean
		{
			return LayerManager.windowLayer.isTop(arg1);
		}
		
		public static function removePopup(arg1:DisplayObject):void
		{
			if (LayerManager.windowLayer.contains(arg1)) 
			{
				LayerManager.windowLayer.removeChild(arg1);
				if (_topDisplayObject == arg1) 
				{
					_topDisplayObject = null;
				}
			}
		}
		
		public static function isPopup(arg1:DisplayObject):Boolean
		{
			return LayerManager.windowLayer.contains(arg1);
		}
		
		public static function setTop(arg1:DisplayObject):void
		{
			if (_topDisplayObject == arg1) 
			{
				return;
			}
			_topDisplayObject = arg1;
			LayerManager.windowLayer.setTop(arg1);
			return;
		}
		
		private static function createTempCover(arg1:DisplayObject, arg2:uint=0, arg3:Number=0.5, arg4:Boolean=false):void
		{
			var v:DisplayObject;
			var color:uint=0;
			var alpha:Number=0.5;
			var mouseEnabled:Boolean=false;
			var parent:DisplayObjectContainer;
			var back:Sprite;
			var rect:Rectangle;
			var removeHandler:Function;
			
			var loc1:*;
			parent = null;
			back = null;
			removeHandler = null;
			v = arg1;
			color = arg2;
			alpha = arg3;
			mouseEnabled = arg4;
			removeHandler = function (arg1:Event):void
			{
				parent.removeEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
				if (parent.contains(back)) 
				{
					parent.removeChild(back);
				}
				return;
			}
			parent = v.parent;
			back = new Sprite();
			rect = localRectToContent(new Rectangle(0, 0, parent.stage.stageWidth, parent.stage.stageHeight), parent.stage, parent);
			back.graphics.beginFill(color, alpha);
			back.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
			back.graphics.endFill();
			back.mouseEnabled = mouseEnabled;
			parent.addChildAt(back, parent.getChildIndex(v));
			v.addEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
			return;
		}
		
		private static function localRectToContent(arg1:Rectangle, arg2:DisplayObject, arg3:DisplayObject):Rectangle
		{
			if (arg2 == arg3) 
			{
				return arg1;
			}
			var loc1:*=localToContent(arg1.topLeft, arg2, arg3);
			var loc2:*=localToContent(arg1.bottomRight, arg2, arg3);
			return new Rectangle(loc1.x, loc1.y, loc2.x - loc1.x, loc2.y - loc1.y);
		}
		
		public static function localToContent(arg1:Point, arg2:DisplayObject, arg3:DisplayObject):Point
		{
			if (arg3 && arg2) 
			{
				return arg3.globalToLocal(arg2.localToGlobal(arg1));
			}
			if (arg2) 
			{
				return arg2.localToGlobal(arg1);
			}
			if (arg3) 
			{
				return arg3.globalToLocal(arg1);
			}
			return null;
		}
		
		public static function closeTopWindow():void
		{
			var loc1:*=LayerManager.windowLayer;
			if (loc1.numChildren == 0) 
			{
				return;
			}
			var loc2:*=loc1.getChildAt((loc1.numChildren - 1)) as IView;
			if (loc2) 
			{
				loc2.hide();
			}
			return;
		}
		
	}
}